<template>
    <el-dialog v-model="visible" :title="`${paramsProps.title}`" :destroy-on-close="true" width="580px" draggable>
      <el-form
        ref="ruleFormRef"
        label-width="140px"
        label-suffix=" :"
        :rules="rules"
        :model="paramsProps.row"
        @submit.enter.prevent="handleSubmit"
      >
      <el-form-item label="模板名称" prop="title">
        <el-input v-model="paramsProps.row.title" placeholder="请填写模板名称" clearable />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false"> 取消 </el-button>
      <el-button type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
    </el-dialog>
</template>

<script name="PrintForm" setup>
import { ref, reactive } from 'vue';
import {  ElMessage } from 'element-plus';


const visible = ref(false);
const rules = reactive({
    title: [{ required: true, message: '请填写模板名称' }],
});
const paramsProps = ref({
  title: '',
  row: {},
  api: undefined,
  getTableList: undefined
});

// 接收父组件传过来的参数
const acceptParams = (params) => {
  paramsProps.value = params;
  visible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref();
const handleSubmit = () => {
  ruleFormRef.value.validate(async valid => {
    if (!valid) return;
    try {
      await paramsProps.value.api(paramsProps.value.row);
      ElMessage.success({ message: `${paramsProps.value.title}成功！` });
      paramsProps.value.getTableList();
      visible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>